

#import "CalendarXMLReader.h"
#import "IDominoAppDelegate.h"
static NSUInteger parsedCalendarEntriesCounter;

@implementation CalendarXMLReader

@synthesize currentCalendarObject = _currentCalendarObject;
@synthesize currentColumnNumber=_currentColumnNumber;
@synthesize allCalendarNumber = _allCalendarNumber;
@synthesize contentOfCurrentCalendarProperty = _contentOfCurrentCalendarProperty;


#define MAX_CALENDARS 25

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    parsedCalendarEntriesCounter = 0;
	//_parsingSubject=YES;
}

- (NSArray*)parseXMLFileFromData:(NSData *)data parseError:(NSError **)error
{	
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    calendarList=[[NSMutableArray alloc] init];
    [parser parse];
    
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
    }
    
    [parser release];
	return [calendarList autorelease];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    }  
    if ([elementName isEqualToString:@"viewentries"]) {
		 NSNumber *topLevelEntries = [attributeDict valueForKey:@"toplevelentries"];
		self.allCalendarNumber=topLevelEntries;
		return;
	}
	if ([elementName isEqualToString:@"viewentry"]) {		
        parsedCalendarEntriesCounter++;
		if (parsedCalendarEntriesCounter > MAX_CALENDARS) {
			[parser abortParsing];
		}		
        // create an instance of it.
        self.currentCalendarObject = [[Calendar alloc] init];
		[self.currentCalendarObject setUNID:[attributeDict valueForKey:@"unid"]];


    }
        
    if ([elementName isEqualToString:@"entrydata"]) {
        self.currentColumnNumber = [attributeDict valueForKey:@"columnnumber"];
		
    } else if ([elementName isEqualToString:@"text"] || [elementName isEqualToString:@"number"] || [elementName isEqualToString:@"datetime"]) {
        // The contents are collected in parser:foundCharacters:.
		self.contentOfCurrentCalendarProperty = [NSMutableString stringWithCapacity:30];
        
	//else if ([elementName isEqualToString:@"textlist"]) {
	
    //} 
	}
	else {
        self.contentOfCurrentCalendarProperty = nil;
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     
    if (qName) {
        elementName = qName;
    }
    
    if ([elementName isEqualToString:@"text"]) {
		if ([self.currentColumnNumber  isEqualToString:@"5"]) {
			if(self.contentOfCurrentCalendarProperty==nil || self.contentOfCurrentCalendarProperty==@"")
				return;
			else if(self.currentCalendarObject.subject == @"" || self.currentCalendarObject.subject==nil)
			{
				self.currentCalendarObject.subject=self.contentOfCurrentCalendarProperty;
			}else{
				self.currentCalendarObject.location=self.contentOfCurrentCalendarProperty;
			}
		}
	
    }
	else if ([elementName isEqualToString:@"number"]) {
		if ([self.currentColumnNumber isEqualToString:@"1"]) {
			if(self.currentCalendarObject.meetingType==0)
				self.currentCalendarObject.meetingType=[self.contentOfCurrentCalendarProperty intValue];
		}
        
    }
	else if ([elementName isEqualToString:@"datetime"]) {
		if ([self.currentColumnNumber isEqualToString:@"0"]) {
			NSMutableString *calendarBeginDateString=[[NSMutableString alloc] init];
			[calendarBeginDateString setString:self.contentOfCurrentCalendarProperty];
			[calendarBeginDateString  insertString:@"-" atIndex:4];
			[calendarBeginDateString  insertString:@"-" atIndex:7];
			NSRange range={10,1};
			[calendarBeginDateString	replaceCharactersInRange:range withString:@" "];
			[calendarBeginDateString  insertString:@":" atIndex:13];
			[calendarBeginDateString  insertString:@":" atIndex:16];
			NSRange range2={19,6};
			NSRange range3={23,2};
			NSString* timezone1=[NSString stringWithFormat:@" +%@00", [calendarBeginDateString substringWithRange:range3]];
			[calendarBeginDateString	replaceCharactersInRange:range2 withString:timezone1];
			
			self.currentCalendarObject.beginTime=[[NSDate alloc] initWithString: calendarBeginDateString ];//2008?08?08?10:01:53,42+10    YYYY-MM-DD HH:MM:SS ±HHMM
						
			[calendarBeginDateString release];
		}
		else if ([self.currentColumnNumber isEqualToString:@"2"]) {
			if([[self.currentCalendarObject.beginTime description] isEqualToString:self.contentOfCurrentCalendarProperty])
				printf("@erorr");
		}
		else {
			
		}
    }
	else if([elementName isEqualToString:@"viewentry"]){
		[calendarList addObject:self.currentCalendarObject];
		return;
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.contentOfCurrentCalendarProperty) {
		[self.contentOfCurrentCalendarProperty appendString:string];
        // If the current element is one whose content we care about, append 'string'
        // to the property that holds the content of the current element.
       // [self.contentOfCurrentEarthquakeProperty appendString:string];
    }
}

@end
